package com.ruoyi.util;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RedisManager {

    private static RedisCache redisCache;

    @Autowired
    public RedisManager(RedisCache redisCache) {
        RedisManager.redisCache = redisCache;
    }

    //通过token获取缓存中登陆的用户信息
    public static LoginUser getLoginUser(String token) {

        try {
            LoginUser user = redisCache.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY+token);
            return user;
            }
        catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    public static Long getCashUserDeptId(Long userId,Long dept){
        String verifyKey = CacheConstants.SYS_USER_DEPT_ID_KEY + userId;
        Long cashDeptId = redisCache.getCacheObject(verifyKey);
        if(ObjectUtil.isNull(cashDeptId)){
            cashDeptId = dept;
        }
        System.out.println("cashDeptId:"+cashDeptId);
        return cashDeptId;
    }

}
